package com.bytz.modules.cms.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.entity.ProductHierarchy;
import com.bytz.modules.cms.product.mapper.ProductHierarchyMapper;
import com.bytz.modules.cms.product.service.IProductHierarchyService;
import com.bytz.modules.cms.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 莱宝CMS—产品管理—产品分类表 服务实现类
 * </p>
 *
 * @author KyleWang
 * @since 2022-10-10
 */
@Service
public class ProductHierarchyServiceImpl extends ServiceImpl<ProductHierarchyMapper, ProductHierarchy> implements IProductHierarchyService {

    @Autowired
    private IProductService productService;

    @Override
    public List<ProductHierarchy> getByHierarchyNumbers(Collection<String> numbers) {

        if (CollectionUtil.isEmpty(numbers)) {
            return Collections.emptyList();
        }

        return lambdaQuery().in(ProductHierarchy::getHierarchyNumber, numbers).list();
    }

    @Override
    @Deprecated
    public Set<String> getNotExistHierarchy(Collection<String> numbers) {
        if (CollectionUtil.isEmpty(numbers)) {
            throw new BytzBootException("hierarchyNumbers为空");
        }
        Set<String> dbHierarchies = lambdaQuery()
                .select(ProductHierarchy::getHierarchyNumber)
                .list()
                .stream().map(ProductHierarchy::getHierarchyNumber)
                .collect(Collectors.toSet());

        return numbers.stream().map(importNumber -> {
            if (dbHierarchies.contains(importNumber)) {
                return null;
            } else {
                return importNumber;
            }
        }).filter(Objects::nonNull).collect(Collectors.toSet());
    }

    @Override
    public Set<String> getAllHierarchy() {
        return lambdaQuery()
                .select(ProductHierarchy::getHierarchyNumber)
                .list()
                .stream().map(ProductHierarchy::getHierarchyNumber)
                .collect(Collectors.toSet());
    }

    @Override
    public Map<String, ProductHierarchy> getByProductIds(Collection<String> productIds) {

        if (CollectionUtil.isEmpty(productIds)) {
            return Collections.emptyMap();
        }

        List<Product> products = productService.getByIds(productIds);

        Set<String> numbers = products.stream().map(Product::getHierarchyNumber).collect(Collectors.toSet());

        Map<String, ProductHierarchy> numberMap = getByHierarchyNumbers(numbers)
                .stream().collect(Collectors.toMap(ProductHierarchy::getHierarchyNumber, t -> t));

        return products.stream().collect(Collectors.toMap(Product::getId, product -> numberMap.get(product.getHierarchyNumber())));
    }
}